package com.ibaodashi.opencvdemo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                convert2Gray();
                Intent intent = new Intent();
                intent.setClassName(MainActivity.this, "com.ibaodashi.gxing.activity.CaptureActivity");
                startActivity(intent);
            }
        });

        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageView viewById = findViewById(R.id.iv);
                viewById.setImageResource(R.mipmap.hei);
            }
        });
    }

//    private void convert2Gray() {
//        Mat src = new Mat();
//        Mat lightness = new Mat();
//        Mat grayscale = new Mat();
//        Mat filter = new Mat();
//        Mat dst = new Mat();
//        Bitmap image = BitmapFactory.decodeResource(this.getResources(), R.mipmap.hei);//从bitmap中加载进来的图像有时候有四个通道，所以有时候需要多加一个转化
//        Utils.bitmapToMat(image, src);//把image转化为Mat
////        Core.multiply(src, new Scalar(1.25, 1.25, 1.25), lightness);
//        Imgproc.cvtColor(src, grayscale, Imgproc.COLOR_BGR2GRAY);//这里由于使用的是Imgproc这个模块所有这里要这么写
////        Imgproc.cvtColor(lightness, grayscale, Imgproc.COLOR_GRAY2BGR);
////        Imgproc.threshold(grayscale, dst, 100, 255, Imgproc.THRESH_BINARY_INV | Imgproc.THRESH_OTSU);
//        Imgproc.medianBlur(grayscale, filter, 7);
//        Imgproc.adaptiveThreshold(filter, dst, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 85, 3);
////        Imgproc.threshold(filter, dst, 140, 255, Imgproc.THRESH_BINARY );
//
//        Utils.matToBitmap(dst, image);//把mat转化为bitmap
//        ImageView imageView = findViewById(R.id.iv);
//        imageView.setImageBitmap(image);
//        //release
//        src.release();
//        grayscale.release();
//        dst.release();
//    }


    @Override
    public void onResume() {
        super.onResume();
//        OpenCVLoader.initDebug();
    }

    private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS: {

                }
                break;
                default: {
                    super.onManagerConnected(status);
                }
                break;
            }
        }
    };
}